package com.panda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panda.entity.map.UserRole;
import com.panda.entity.po.Role;
import com.panda.entity.vo.MenuVo;
import com.panda.entity.vo.RoleVo;
import com.panda.mapper.MenuMapper;
import com.panda.mapper.RoleMapper;
import com.panda.service.RoleService;
import com.panda.service.UserRoleService;
import com.panda.utils.BeanCopyUtil;
import com.panda.utils.RespondResult;
import com.panda.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.role;

@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 返回管理员角色名
     * @param id 用户id
     * @return 对应管理员角色名
     */
    @Override
    public RespondResult getRoleName() {
        Long userId = SecurityUtils.getUserId();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        UserRole userRole = userRoleService.getOne(wrapper);
        Role role = getById(userRole.getRoleId());
        if (Objects.nonNull(role)){
            RoleVo roleVo = BeanCopyUtil.copyBean(role, RoleVo.class);
            return RespondResult.okResult(roleVo);
        }
        return RespondResult.errorResult(404,"找不到该管理员名字");
    }

    @Override
    public RespondResult getRoleRoutes() {
        Long userId = SecurityUtils.getUserId();
        List<MenuVo> menuVos = menuMapper.selectRoutesByUserId(userId);
        System.out.println(menuVos);
        if (Objects.nonNull(menuVos)){
            return RespondResult.okResult(menuVos);
        }
        return RespondResult.errorResult(404,"找不到相关路由");
    }
}
